package com.wn.sk.order.core.controller;


import com.wn.common.api.Result;
import com.wn.common.api.order.dto.OrderDto;
import com.wn.common.api.order.vo.OrderVo;
import com.wn.common.api.status.impl.ResultCode;
import com.wn.sk.order.core.service.IOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 订单表 前端控制器
 * </p>
 *
 * @author yellow
 * @since 2022-09-08
 */
@RestController
@RequestMapping("/order")
@Api(tags = "订单列表")
public class OrderController {

    @Autowired
    private IOrderService orderService;

    @ApiOperation("测试订单模块....")
    @GetMapping
    public String hello(){
        return "hello order";
    }

    @ApiOperation("生成订单....")
    @PostMapping
    public Result<String> createOrder(@RequestBody OrderDto dto){

        //创建一个订单
        String orderSn = orderService.createOrder(dto);

        return Result.common(orderSn);
    }

    @GetMapping("/{orderSn}")
    @ApiOperation("根据订单编号查询订单信息")
    public Result<OrderVo> getOrderBySn(
            @PathVariable("orderSn")
            @ApiParam(name="orderSn",value = "订单编号") String orderSn){
        OrderVo ordervo = orderService.findByOrderSn(orderSn);
        return Result.common(ordervo);
    }

    @PutMapping("/auth/order/{orderSn}")
    public Result update(@PathVariable("orderSn") String orderSn){

        Integer status = null;
        try {
            status = orderService.updateByOrderSn(orderSn);
        } catch (Exception e) {
            return Result.error(200000,"更新失败...");
        }
        return Result.common(status);
    }
}

